package com.block.channel.flow.channel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channel.flow.IChannelReportService;
import com.block.channel.model.result.ReprotFlowResult;
import com.block.common.bean.CommonParam;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelBaseInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class JiZhunChannelReportService implements IChannelReportService {

    private static Logger log = LogFactory.getCommLog();

    private String requestUrl = "https://api-extra.hzjizhun.com/hdt-trk/hdt/adv-cvt";
    private String authStr = "NjE5YzVhNzFmY2U1YjBlNDcxMjI3Y2ViOmR5dUNQVUJTZ0NWTHkzanNTY3dVUUVHV3lDaktjd2xG";

    /**
     * devKey:619c5a71fce5b0e471227ceb
     * devSecret:dyuCPUBSgCVLy3jsScwUQEGWyCjKcwlF
     * base64String:NjE5YzVhNzFmY2U1YjBlNDcxMjI3Y2ViOmR5dUNQVUJTZ0NWTHkzanNTY3dVUUVHV3lDaktjd2xG
     */

    @Override
    public ReprotFlowResult registerActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        ReprotFlowResult flowResult = new ReprotFlowResult();
        flowResult.setSuccess(false);

        JSONObject headObject = commonParam.getHeadObject();
        String jcid = headObject.getString("jcid");
        if (StrUtil.isEmpty(jcid)){
            flowResult.setCode("-2");
            flowResult.setMessage("jcid参数为空，不做处理");
            return flowResult;
        }

        String authContent = StrUtil.format("Basic {}", authStr);

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("jcid", jcid);
        bizMap.put("event", "event1");

        log.info("【极准上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .header("Authorization", authContent)
                .body(JSON.toJSONString(bizMap))
                .execute().body();

        log.info("【极准上报】【注册】【开始结束】【result】{}", response);

        JSONObject result = JSON.parseObject(response);
        Integer code = result.getInteger("code");
        String message = result.getString("message");

        flowResult.setCode(String.valueOf(code));
        flowResult.setMessage(message);
        flowResult.setParamInfo(JSON.toJSONString(bizMap));
        flowResult.setResultInfo(response);
        if (ObjectUtil.equal(code, Integer.valueOf(200))){
            // 上报不成功报异常
            flowResult.setSuccess(true);
        }else{
            flowResult.setSuccess(false);
        }
        return flowResult;
    }

    @Override
    public ReprotFlowResult downloadActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }

    @Override
    public ReprotFlowResult payBuyActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }


}
